<!DOCTYPE html>
<html lang="cmn-Hans">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>快联快报</title>
    <style>
        body {
            color: #444;
            font-size: 14px;
            line-height: 1.5;
            display: flex;
            justify-content: center;
        }

        main {
            display: flex;
            flex-grow: 1;
            height: 500px;
            gap: 10px 0;
            position: relative;
            margin: 20px;
            max-width: 1150px;
            justify-content: space-between;
        }

        #raw,
        #raw:focus,
        #result {
            border: 1px solid #c3c3c3;
            border-radius: 5px;
        }

        #raw,
        #result {
            flex: 1;
            padding: 30px 30px;
        }

        #raw {
            box-sizing: border-box;
            resize: none;
        }

        #copy {
            position: absolute;
            top: 5px;
            right: 5px;
            display: block;
            content: '复制';
            padding: 5px;
            cursor: pointer;
            border: 1px solid #c3c3c3;
            border-radius: 2px;
        }

        #tool-bar {
            display: flex;
            flex-direction: column;
            width: 20px;
            flex-basis: content;
            min-width: 50px;
            justify-content: center;
        }

        button {
            border: #c3c3c3 1px solid;
            border-radius: 2px;
            margin: 10px;
            height: 30px;
            cursor: pointer;
            padding: 0 10px;
        }
    </style>
</head>

<body>
    <main>
        <textarea name="source" id="raw" placeholder="在这里输入链接或者长文字" autofocus></textarea>
        <div id="tool-bar">
            <button type="button" id="shortLinkBtn">短链 </button>
            <button type="button" id="textBtn">文本</button>
        </div>
        <div id="result">
            <div id="copy">复制</div>
            <div id="result-text"></div>
        </div>
    </main>
    <script src="https://www.unpkg.com/jquery@3.7.1/dist/jquery.min.js"></script>
    <script>
        const source = $('#raw')
        const result = $('#result-text')
        const linkBtn = $('#shortLinkBtn')
        const textBtn = $('#textBtn')
        const copyBtn = $('#copy')

        linkBtn.on('click', function () {
            const sourceText = source.val()

            $.ajax({
                url: '/s/',
                type: 'POST',
                data: JSON.stringify({
                    link: sourceText.trim()
                }),
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                headers: {
                    'X-AUTH-TOKEN': '<%=AUTH_TOKEN%>'
                },
                success: function (rst) {
                    if (rst) {
                        result.text(rst.link)
                    }
                },
                error: function (error) {
                    alert(error);
                }
            });


        })

        textBtn.on('click', function () {
            const sourceText = source.val()

            $.ajax({
                url: '/p/',
                type: 'POST',
                data: JSON.stringify({
                    text: sourceText.trim()
                }),
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                headers: {
                    'X-AUTH-TOKEN': '<%=AUTH_TOKEN%>'
                },
                success: function (rst) {
                    if (rst) {
                        result.text(rst.url)
                    }
                },
                error: function (error) {
                    alert(error);
                }
            });


        })

        copyBtn.on('click', function () {
            const text = result.text()
            // 复制text到剪贴板 
            navigator.clipboard.writeText(text).catch(e => alert(e));
            alert('复制成功')
        })
    </script>
</body>

</html>